package com.wechat.page.contact.card;

import com.swak.entity.Toggle;
import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.Display;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UIImage;
import com.swak.fx.support.ui.UIRadioButton;
import com.swak.fx.support.ui.UITextFlow;
import com.swak.utils.StringUtils;
import com.wechat.dto.ContactDTO;
import com.wechat.page.comps.TextComp;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;

/**
 * 联系人 - 缩略
 * 
 * @author 618lf
 */
@FXMLView(title = "联系人 - 缩略", value = "ContactCardComp.fxml", css = "ContactCardComp.css")
public class ContactCardComp extends AbstractComp implements TextComp {

	public static final String ClickEvent = "Click.Event";
	public static final String ToggleEvent = "Toggle.Event";

	@FXML
	protected UIRadioButton radio;
	@FXML
	protected UIImage image;
	@FXML
	protected UITextFlow name;

	public ContactCardComp(AbstractPage parent) {
		super(parent);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.image.image(Display.logo());
		this.image.setPrefSize(40, 40);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.root.addEventHandler(MouseEvent.MOUSE_CLICKED, this.mouseClickHandler);
	}

	private void onMouseClick(MouseEvent event) {
		this.activeProperty.set(!this.activeProperty.get());
		this.$emit(ClickEvent, this.contactProperty.get());
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.contactProperty().addListener((ob, o, n) -> this.onContactChange(n));
		this.refreshProperty().addListener((ob, o, n) -> this.onChatRefresh(n.longValue()));
		this.activeProperty().addListener((ob, o, n) -> this.$emit(ToggleEvent,
				new Toggle<Object>().setActive(n).setValue(this.contactProperty.get())));
	}

	private void onChatRefresh(Long time) {
		ContactDTO contact = this.contactProperty.get();
		if (contact != null) {
			this.onContactChange(contact);
		}
	}

	private void onContactChange(ContactDTO contact) {
		this.buildTextContent(this.name, StringUtils.defaultIfBlank(contact.getAlias(), contact.getNickName()));
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
		this.radio.managedProperty().bind(this.enableSelectProperty);
		this.radio.visibleProperty().bind(this.enableSelectProperty);
		this.radio.selectedProperty().bind(this.activeProperty);
	}

	@Override
	public void destory() {
		super.destory();
		this.root.removeEventHandler(MouseEvent.MOUSE_CLICKED, this.mouseClickHandler);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final ObjectProperty<ContactDTO> contactProperty = new SimpleObjectProperty<>();
	private final LongProperty refreshProperty = new SimpleLongProperty();

	public ObjectProperty<ContactDTO> contactProperty() {
		return contactProperty;
	}

	public LongProperty refreshProperty() {
		return refreshProperty;
	}

	private final BooleanProperty enableSelectProperty = new SimpleBooleanProperty(false);

	public BooleanProperty enableSelectProperty() {
		return enableSelectProperty;
	}

	private final BooleanProperty activeProperty = new SimpleBooleanProperty(false);

	public BooleanProperty activeProperty() {
		return activeProperty;
	}

	// ================================================================================
	// Event
	// ================================================================================
	EventHandler<MouseEvent> mouseClickHandler = (e) -> this.onMouseClick(e);
}